package GUI;


import java.awt.Component;
import java.io.IOException;
import java.util.Random;

import javax.swing.JFrame;
import client.ChatClient;
import common.ChatIF;
import common.Perror;
import entities.CProcess;
import entities.login;
import Controllers.WorkersController;
import Controllers.StudentController;
import MessagePacks.*;

/**
 *@author Juan Alapin - 324507516
 *@author Giora Yaffe - 036551281
 *@author Shachar Gritzman - 300174026
 *@author Nir Porat - 021515580
*/
public class MainGUI extends JFrame implements  ChatIF {
	public MainGUI() {
	}

	/**
	 * 
	 */
	public static login usr; 
	public static CProcess process;
	private LoginGUI loginUserGUI;
	public ChatClient client; 
	private ClientGUI clientgui; 
	private static final long serialVersionUID = 1L;
	public static final Component mainframe = null;
	private StudentController studctrl;
	private WorkersController mngrctrl; 
	
	/**
	 * 
	 */
	public void start() { 
		
		clientgui = new ClientGUI(this); 
		clientgui.setBounds(100, 100, 450, 300);
	}

	/**
	 * 
	 * @throws IOException
	 */
	public void clientHandler() throws IOException { 
		
		loginUserGUI= new LoginGUI(this);
		loginUserGUI.setBounds(200, 200, 450, 300);
		
	}
	
	/**
	 * 
	 */
	public void loginhandler(){
		
		
		usr= new login(loginUserGUI.getId(), loginUserGUI.getPass());
		LoginPack lgn= new LoginPack(usr); 
		try {
			
			client = new ChatClient(clientgui.getIP(),clientgui.getPort(),this );
			client.handleMessageFromClientUI(lgn);
			lgn= (LoginPack) client.getMessage();
			usr= lgn.getUsr(); 
			handleLogin(usr.getPermissionLvl());
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
	
	/**
	 * 
	 */
	public void logoutHandler(){
		
		LogoutPack lgop = new LogoutPack(usr);
		client.handleMessageFromClientUI(lgop);
		try {
			client.closeConnection();
		} catch (IOException e) {
			e.printStackTrace();
		}
		usr= null;	
	}
	/**
	 * @param args
	 */
	public void handleLogin (int oper){
		
		
		switch(oper) {
		case login.ERROR: 
			Perror.pError("Bad User/Password");
			break;
		case login.STUDENT:
			setStudctrl(new StudentController(client,this));
			break;
		case login.WORKER:
			setMngrctrl(new WorkersController(client,this));	
			break;
		case login.ALREADY_LOGGED_IN:
			Perror.pError("User Is already logged in");
			break; 
		case login.IS_DEP_MANAGER:
			setMngrctrl(new WorkersController(client,this));
			break;
		case login.ADMINISTRATIVE_STAFF:
			setStudctrl(new StudentController(client,this));
			break;
		case login.PROFESOR:
			setStudctrl(new StudentController(client,this));
			break;
		}

	}
	
	/**
	 * 
	 * @param nrp - New request pack
	 */
	 public void handleNewRequest(NewRequestPack nrp)
	{
	
		client.handleMessageFromClientUI(nrp);
	} 
	
	/**
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		MainGUI turnOn = new MainGUI();
		turnOn.start();
		
	}



	public StudentController getStudctrl() {
		return studctrl;
	}

	public void setStudctrl(StudentController studctrl) {
		this.studctrl = studctrl;
	}

	public WorkersController getMngrctrl() {
		return mngrctrl;
	}

	public void setMngrctrl(WorkersController mngrctrl) {
		this.mngrctrl = mngrctrl;
	}

	public void display(String message) {
		// TODO Auto-generated method stub
		
	}
	public double analyzer(){
		Random r = new Random();
		double value = 3 + (6 - 1)*r.nextDouble();
		return value;
		
	}

}
